<template>
  <div>
    <el-dialog
            v-model="show"
            :title="$t('添加设备')"
            width="643"
            @close="closeDeviceDialog"
    >
      <el-form :model="form" :rules="rules" ref="formRef">
        <!-- 摄像头位置 -->
        <el-form-item
                :label="$t('摄像头位置')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="positionId"
        >
          <el-select
                  v-model="form.positionId"
                  :placeholder="$t('请选择摄像头位置')"
                  clearable
          >
            <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.position"
                    :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 设备SN码 -->
        <el-form-item
                :label="$t('设备SN码')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="deviceSn"
        >
          <el-input
                  v-model="form.deviceSn"
                  autocomplete="off"
                  :placeholder="$t('请输入设备SN码')"
          />
        </el-form-item>
        <!-- 启用状态 -->
        <el-form-item
                :label="$t('启用状态')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
        >
          <el-switch
                  v-model="form.deviceEnable"
                  class="ml-2"
                  inline-prompt
                  size="large"
                  style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
                  active-text="Y"
                  inactive-text="N"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDeviceDialog">{{$t('取消')}}</el-button>
          <el-button type="primary" @click="addDevice" class="confirm">
            {{$t('确认')}}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { ElMessage } from 'element-plus';
  import { getDevicePosition, addDevice } from '@/api/index';

  export default {
    props: {},
    data() {
      return {
        options: [], // 摄像头位置选项
        show: true, // 控制弹窗显示
        formLabelWidth: '110px', // 表单标签宽度
        labelPosition: 'left', // 标签位置
        form: {
          positionId: '', // 摄像头位置ID
          deviceSn: '', // 设备SN码
          deviceEnable: true, // 启用状态
        },
        rules: {
          // 摄像头位置校验规则
          positionId: [
            { required: true, message: this.$t('摄像头位置不能为空'), trigger: 'change' },
          ],
          // 设备SN码校验规则
          deviceSn: [
            { required: true, message: this.$t('设备SN码不能为空'), trigger: 'blur' },
            {
              pattern: /^[A-Za-z0-9]{6,20}$/,
              message: this.$t('设备SN码必须为6-20位字母或数字'),
              trigger: 'blur',
            },
          ],
        },
      };
    },
    async mounted() {
      // 获取摄像头位置选项
      const { data } = await getDevicePosition();
      if (data.code === 0) {
        this.options = data.data;
      }
    },
    methods: {
      // 添加设备
      async addDevice() {
        try {
          // 校验表单
          await this.$refs.formRef.validate();
          // 提交数据
          const { data } = await addDevice({
            ...this.form,
            deviceEnable: this.form.deviceEnable ? 'Y' : 'N',
          });
          if (data.code === 0) {
            this.$emit('closeDeviceDialog');
            ElMessage.success(this.$t('设备添加成功'));
            this.$emit('refresh');
          } else {
            ElMessage.error(data.msg);
          }
        } catch (error) {
          console.log(error);
        }
      },
      // 关闭弹窗
      closeDeviceDialog() {
        this.$emit('closeDeviceDialog');
      },
    },
  };
</script>
